"use strict";
const way = require("../utils/way");
const Result = require("../basics/result");

/**
 * 查询列表
 * @param {*} cloud 
 * @param {*} param1 
 */
exports.selectList = async (cloud, session, { template_id, page_num = 1, page_size = 20 }) => {
  let data = { param0: { template_id, page_num, page_size } };
  data.session = session;
  console.log("wedgit params", data);
  const results = await way.taobao(cloud, "taobao.miniapp.widget.template.instance.query", data);
  console.log("wedgit results", results);
  if (!results.result || !results.result.success) {
    return null;
  }
  if (!results.result.model) {
    return null;
  }
  let arrs = results.result.model.miniapp_instance_app_all_versions_d_t_o;
  if (!arrs || arrs.length < 1) {
    return null;
  }
  return arrs[0].app_version_list.mini_app_instance_version_d_t_o;
};

/**
 * 模板实例化
 * @param {*} cloud 
 * @param {*} param1 
 */
exports.instantiate = async (cloud, session, { description, template_id, template_version }) => {
  let data = { param_mini_app_instantiate_template_app_simple_request: { description, template_id, template_version } };
  data.session = session;
  console.log("wedgit instantiate params", data);
  const results = await way.taobao(cloud, "taobao.miniapp.widget.template.instantiate", data);
  console.log("wedgit instantiate results", results);
  const error = results.error_response;
  if (error) {
    throw Result.error(Result.constant.taobaofail, error.sub_msg);
  }
  if (results.result.success) {
    return true;
  }
  throw Result.error(Result.constant.taobaofail, results.result.err_message);
};

/**
 * 更新实例化
 * @param {*} cloud 
 * @param {*} param1 
 */
exports.updateapp = async (cloud, session, { entity_id, template_id, template_version }) => {
  let data = { param_mini_app_instantiate_template_app_update_request: { entity_id, template_id, template_version } };
  data.session = session;
  console.log("wedgit updateapp params", data);
  const results = await way.taobao(cloud, "taobao.miniapp.widget.template.instance.update", data);
  console.log("wedgit updateapp results", results);
  const error = results.error_response;
  if (error) {
    throw Result.error(Result.constant.taobaofail, error.sub_msg);
  }
  if (results.result.success) {
    return true;
  }
  throw Result.error(Result.constant.taobaofail, results.result.err_message);
};

// /**
//  * 上线实例化应用
//  * @param {*} cloud 
//  * @param {*} param1 
//  */
// exports.onlineapp = async (cloud,session, { clients, app_id, app_version, template_id, template_version }) => {
//   let data = { clients, app_id, app_version, template_id, template_version };
//   data.session = session;
//   console.log("temp onlineapp params", data);
//   const results = await way.taobao(cloud, "taobao.miniapp.template.onlineapp", data);
//   console.log("temp onlineapp results", results);
//   const error = results.error_response;
//   if (error) {
//     throw Result.error(Result.constant.taobaofail, error.sub_msg);
//   }
//   return true;
// };

